<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-col :span="12">
          <el-form-item label="出库日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              range-separator="-"
              style="width: 215px"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="领料单号" prop="wfmDjbh">
            <el-input
              v-model="queryParams.wfmDjbh"
              placeholder="请输入领料单号"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="物料代码" prop="wfdWldm">
            <el-input
              style="width: 580px"
              v-model="queryParams.wfdWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="出库仓库" prop="ckDm">
            <el-select
              v-model="queryParams.ckDm"
              style="width: 580px"
              multiple
              clearable
              placeholder="请选择出库仓库"
            >
              <el-option
                clearable
                v-for="dict in allStorageOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:souringReport:export']"
          @click="handleExcel(1)"
          size="small"
        >
          明细数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:souringReport:export']"
          @click="handleExcel(2)"
          size="small"
        >
          仓库统计
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['output:souringReport:export']"
          @click="handleExcel(3)"
          size="small"
        >
          厂商统计
        </el-button>
      </el-col>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          border
          stripe
          :height="tableHChild"
          @scrollBottom="load"
          :data="wfdDetailList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="领料单号"
            align="center"
            width="135"
            prop="wfmDjbh"
          />
          <el-table-column
            label="领料类型"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="wfmType"
          >
            <template #default="scope">
              <dict-tag :options="wfmTypeOptions" :value="scope.row.wfmType" />
            </template>
          </el-table-column>
          <el-table-column
            label="部门名称"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="wfmBmmc"
          />
          <el-table-column
            label="仓库"
            class-name="rkck"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="wfmCkdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allStorageOptions"
                :value="scope.row.wfmCkdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="厂商代码"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="wfmCsdm"
          >
            <template #default="scope">
              <dict-tag :options="allCsOptions" :value="scope.row.wfmCsdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="销售单号"
            width="135"
            align="center"
            prop="wfdSodh"
          />
          <el-table-column
            label="物料代码"
            width="135"
            align="center"
            prop="wfdWldm"
          />
          <el-table-column
            label="品名规格"
            width="285"
            show-overflow-tooltip
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="最小包装"
            width="80"
            align="center"
            prop="wfdMinbz"
          />
          <el-table-column
            label="申请数量"
            width="80"
            align="center"
            prop="wfdSqsl"
          />
          <el-table-column
            label="出库数量"
            width="80"
            align="center"
            prop="wfdCksl"
          />
          <el-table-column
            label="存放位置"
            width="100"
            align="center"
            prop="wfdCwdm"
          >
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.wfdCwdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="原因代码"
            width="80"
            align="center"
            prop="wfmYydm"
          />
          <el-table-column
            label="原因描述"
            width="80"
            align="center"
            prop="wfmYyms"
          />
          <el-table-column
            label="备注"
            width="80"
            align="center"
            prop="wfmDesc"
          />
          <el-table-column
            label="开单人员"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="wfmJlry"
          />
          <el-table-column
            label="开单日期"
            :show-overflow-tooltip="true"
            align="center"
            width="150"
            prop="wfmJlrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.wfmJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="审核人员"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
            prop="wfmShry"
          />
          <el-table-column
            label="审核日期"
            width="150"
            :show-overflow-tooltip="true"
            align="center"
            prop="wfmShrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.wfmShrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="出库人员"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
            prop="wfdCkry"
          />
          <el-table-column
            label="出库日期"
            width="150"
            :show-overflow-tooltip="true"
            align="center"
            prop="wfdCkrq"
          />
          <el-table-column
            label="过账人员"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
            prop="wfmGzry"
          />
          <el-table-column
            label="过账日期"
            width="150"
            :show-overflow-tooltip="true"
            align="center"
            prop="wfmGzrq"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按仓库统计" name="second">
        <el-table
          border
          stripe
          :height="tableHChild"
          @scrollBottom="load2"
          :data="wfdCkList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="仓库"
            class-name="rkck"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="wfmCkdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allStorageOptions"
                :value="scope.row.wfmCkdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            width="135"
            align="center"
            prop="wfdWldm"
          />
          <el-table-column
            label="品名规格"
            width="285"
            show-overflow-tooltip
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="最小包装"
            width="80"
            align="center"
            prop="wfdMinbz"
          />
          <el-table-column
            label="申请数量"
            width="80"
            align="center"
            prop="wfdSqsl"
          />
          <el-table-column
            label="出库数量"
            width="80"
            align="center"
            prop="wfdCksl"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按厂商统计" name="third">
        <el-table
          border
          stripe
          :height="tableHChild"
          @scrollBottom="load3"
          :data="wfdCsList"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="厂商代码"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="wfmCsdm"
          >
            <template #default="scope">
              <dict-tag :options="allCsOptions" :value="scope.row.wfmCsdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            width="135"
            align="center"
            prop="wfdWldm"
          />
          <el-table-column
            label="品名规格"
            width="285"
            show-overflow-tooltip
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="80"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="最小包装"
            width="80"
            align="center"
            prop="wfdMinbz"
          />
          <el-table-column
            label="申请数量"
            width="80"
            align="center"
            prop="wfdSqsl"
          />
          <el-table-column
            label="出库数量"
            width="80"
            align="center"
            prop="wfdCksl"
          />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getAll } from "@/api/repertory/Storage";
import { exportwfmReport, wfmList } from "@/api/output/souringReport";
import { getAllCs } from "@/api/putStorage/putStorageAdd";

export default {
  name: "SouringReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      tableHChild: document.body.clientHeight - 280,
      activeTab: "first",
      wfdDetailList: [],
      wfdCsList: [],
      wfdCkList: [],
      // 显示搜索条件
      openSearch: false,
      // 总条数
      total: 0,
      total2: 0,
      total3: 0,
      //所有存储仓库
      allStorageOptions: [],
      wfmTypeOptions: [],
      //所有存储储位
      allCwOptions: [],
      //单位字典
      unitFlagOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //所有厂商
      allCsOptions: [],
      // 建立日期时间范围
      dateRange: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        wfdWldm: null,
        wfmDjbh: null,
        ckDm: [],
      },
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //厂商
    getAllCs().then((response) => {
      this.allCsOptions = response.data;
      for (let item of this.allCsOptions) {
        item.dictLabel = item.csmCs;
        item.listClass = "default";
        item.dictValue = item.csmCsdm;
      }
    });
    //储位
    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });
    //领料类型
    this.getDicts("outsourcing_requisition_type").then((response) => {
      this.wfmTypeOptions = response.data;
    });
    //仓库
    getAll().then((response) => {
      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    this.getList();
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginWfdCkrq"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endWfdCkrq"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.wfdDetailList = [];
        this.wfdCkList = [];
        this.wfdCsList = [];
        this.listed();
      }
    },
    listed() {
      wfmList(this.queryParams).then((response) => {
        //明细
        this.wfdDetailList = this.wfdDetailList.concat(
          response.data.list1.rows
        );
        this.total = response.data.list1.total;
        //仓库
        this.wfdCkList = this.wfdCkList.concat(response.data.list2.rows);
        this.total2 = response.data.list2.total;
        //仓库
        this.wfdCsList = this.wfdCsList.concat(response.data.list3.rows);
        this.total3 = response.data.list3.total;
        this.loading = false;
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },

    load2() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total2 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },

    load3() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total3 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        this.listed();
      }
      return 0;
    },

    handleSearch() {
      this.openSearch = true;
      this.title = "查询来料进货统计分析";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    //导出Excel
    handleExcel(indexExcel) {
      let queryParams = this.queryParams;
      queryParams.index = indexExcel;
      exportwfmReport(queryParams).then((response) => {
        this.download(response.msg);
      });
    },
  },
};
</script>
